package cn.wolfcode.p2p.website.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.github.pagehelper.PageInfo;

import cn.wolfcode.p2p.base.util.Constants;
import cn.wolfcode.p2p.business.domain.BidRequest;
import cn.wolfcode.p2p.business.query.BidRequestQueryObject;
import cn.wolfcode.p2p.business.query.CreditTransferQueryObject;
import cn.wolfcode.p2p.business.service.IBidRequestService;
import cn.wolfcode.p2p.business.service.ICreditTransferService;

/**
 * 首页控制器
 * 
 * @author wolfcode
 *
 */
@Controller
public class IndexController {

	@Autowired
	private IBidRequestService bidRequestService;
	
	@Autowired
	private ICreditTransferService transferService;

	/**
	 * 展示投资列表页面框架
	 */
	@RequestMapping("invest")
	public String investPage() {
		return "invest";
	}

	/**
	 * 投资列表列表内容
	 */
	@RequestMapping("invest_list")
	public String investList(BidRequestQueryObject qo, Model model) {
		// 如果是信用标
		if (qo.getBidRequestType() == Constants.BIDREQUEST_TYPE_NORMAL) {
			// 如果选择的是全部,
			if (qo.getState() == -1) {
				qo.setStates(new int[] { Constants.BIDREQUEST_STATE_BIDDING, Constants.BIDREQUEST_STATE_PAYING_BACK,
						Constants.BIDREQUEST_STATE_COMPLETE_PAY_BACK });
			}
			model.addAttribute("pageResult", this.bidRequestService.query(qo));
			return "invest_list";
		} else {
			CreditTransferQueryObject cto=new CreditTransferQueryObject();
			cto.setPageSize(qo.getPageSize());
			cto.setCurrentPage(qo.getCurrentPage());
			cto.setState(Constants.BIDREQUEST_STATE_BIDDING);
			// 债权转让标
			model.addAttribute("pageResult",this.transferService.query(cto));
			return "credittrans/credittransfer_list";
		}
	}

	/**
	 * 显示首页
	 */
	@RequestMapping("main")
	public String indexPage(Model model) {

		// 投资列表
		BidRequestQueryObject qo = new BidRequestQueryObject();
		qo.setCurrentPage(1);
		qo.setPageSize(5);
		qo.setStates(new int[] { Constants.BIDREQUEST_STATE_BIDDING, Constants.BIDREQUEST_STATE_PAYING_BACK,
				Constants.BIDREQUEST_STATE_COMPLETE_PAY_BACK });
		qo.setOrderBy("bidrequeststate");
		qo.setOrderType("ASC");

		PageInfo<BidRequest> pi = this.bidRequestService.query(qo);
		model.addAttribute("bidRequests", pi.getList());

		// 处于发标中的借款列表
		qo.setState(Constants.BIDREQUEST_STATE_PUBLISH_PENDING);
		qo.setStates(null);
		model.addAttribute("publishPendngBidRequests", this.bidRequestService.query(qo).getList());

		return "main";
	}

}
